package cn.chiship.commons.aspect;

import java.lang.reflect.Method;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import cn.chiship.commons.annotation.SystemLogAnnotation;
import cn.chiship.commons.bean.CommonAdmin;
import cn.chiship.commons.bean.CommonLog;
import cn.chiship.commons.constant.CommonConstant;
import cn.chiship.commons.util.CommonUtil;
import cn.chiship.commons.util.IPUtil;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;



import com.alibaba.fastjson.JSON;

@Aspect
public class LogAspect {
	private static Logger _log =Logger.getLogger(LogAspect.class);
	// 开始时间
	private long startTime = 0L;
	// 结束时间
	private long endTime = 0L;
	@Pointcut("execution(* cn.chiship.lv.controller..*.*(..))")
    private void controllerAspect(){}

	
	@Before("controllerAspect()")
	public void doBeforeInServiceLayer(JoinPoint joinPoint) {
		_log.debug("doBeforeInServiceLayer");
		startTime = System.currentTimeMillis();
	}

	@After("controllerAspect()")
	public void doAfterInServiceLayer(JoinPoint joinPoint) {
		_log.debug("doAfterInServiceLayer");
	}
	@Around("controllerAspect()")
	public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
		CommonLog log=new CommonLog();
		/**
		 * 获取request 
		 */
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		/**
		 * 获取session
		 */
		HttpSession session=request.getSession();
		/**
		 * 获取系统用户
		 */
		CommonAdmin admin=(CommonAdmin) session.getAttribute(CommonConstant.SESSION_LOGIN_USER_ADMIN);
		if(admin==null){
            log.setUsername("--");
        }else{
            log.setUsername(admin.getUserName());
        }
		/**
		 * 从注解中获取操作名称、获取响应结果 
		 */
		Object result = pjp.proceed();
		Signature signature = pjp.getSignature();
		MethodSignature methodSignature = (MethodSignature) signature;
		Method method = methodSignature.getMethod();
		if (method.isAnnotationPresent(SystemLogAnnotation.class)) {
            SystemLogAnnotation systemlog = method.getAnnotation(SystemLogAnnotation.class);
            log.setDescription(systemlog.module()+"["+systemlog.methods()+"]");
        } else {
            log.setDescription("此操作不包含注解");
        }
		endTime = System.currentTimeMillis();
		log.setBasePath(CommonUtil.getBasePath(request));
		log.setIp(IPUtil.getIpAddr(request));
		log.setMethod(request.getMethod());
		log.setParameter(JSON.toJSONString(request.getParameterMap()));
        log.setResult(JSON.toJSONString(result));
        log.setSpendTime((int) (endTime - startTime));
        log.setStartTime(startTime);
        log.setUri(request.getRequestURI());
        log.setUrl(request.getRequestURL().toString());
		log.setUserAgent(request.getHeader("User-Agent"));
		System.out.println(JSON.toJSON(log));
		return result;
	}


	


}
